package com.github.shiro.controller;


import com.github.shiro.entity.User;
import com.github.shiro.service.IUserService;
import com.github.shiro.utils.Constant;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ruiyanglee
 * @since 2020-07-23
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    @RequestMapping(value = "/add", method = RequestMethod.PUT)
    public String add(User user) {
        String password = user.getPassword();
        user.setPrivateSalt(Constant.PRIVATE_SALT);
        // 对密码进行加密
        Md5Hash md5Hash = new Md5Hash(password, Constant.PRIVATE_SALT, 1);
        user.setPassword(md5Hash.toString());
        userService.save(user);
        System.out.println(user.toString());
        return "添加成功";
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<User> list() {
        return userService.findAll();
    }

    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    public void del(@RequestParam Long id) {
        userService.removeById(id);
    }
}

